package cn.bgs.service.impl;

import cn.bgs.common.R;
import cn.bgs.mapper.PaymentTypeMapper;
import cn.bgs.pojo.PaymentType;
import cn.bgs.pojo.User;
import cn.bgs.service.IPaymentTypeService;
import cn.bgs.vo.Bill;
import cn.bgs.vo.Livingexpenses;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

import java.util.List;

/**
 * <p>
 * 缴费类型表(账单表) 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class PaymentTypeServiceImpl extends ServiceImpl<PaymentTypeMapper, PaymentType> implements IPaymentTypeService {

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Override
    public List<Livingexpenses> cfMypayment(Integer id) {
        return paymentTypeMapper.cfMypayment(id);
    }

    @Override
    public int cfInsertPayment(PaymentType paymentType) {
        //      获取6位随机数
        Random random = new Random();
        String result="";
        for (int i=0;i<8;i++) {
            result+=random.nextInt(10);
        }
        paymentType.setPayNumber(result);
        return paymentTypeMapper.cfInsertPayment(paymentType);
    }

    @Override
    public User getUsermoney(Integer id) {
        return paymentTypeMapper.getUsermoney(id);
    }

    @Override
    @Transactional
    public int cfPayment(Double paybalance,Integer id) {
        return paymentTypeMapper.cfPayment(paybalance,id);
    }

    @Override
    public void uupdatePaymentType(Integer payId) {
        paymentTypeMapper.uupdatePaymentType(payId);
    }

    @Override
    @Transactional
    public int cfRecharge(Double newmoney, Integer id) {
        return paymentTypeMapper.cfRecharge(id,newmoney);
    }

    @Override
    public List<PaymentType> cfGetAllcost(Integer id,String type,String month) {
        return paymentTypeMapper.cfGetAllcost(id,type,month);
    }


    @Override
    public R findBill(String paymentState, String type, String nameType, String name) {

        List<Bill> bills = paymentTypeMapper.findBill(paymentState, type, nameType, name);

        return R.ok().data("bills",bills);
    }

    @Override
    public R delBiil(Integer paymentTypeId) {

        Integer i = paymentTypeMapper.delBill(paymentTypeId);

        if(i!=0){
            return R.ok();
        }else {
            return R.error().data("error","删除失败");
        }
    }

    @Override
    public R addBill(Bill bill) {
        Integer i = paymentTypeMapper.addBill(bill);

        if(i!=0){
            return R.ok();
        }else {
            return R.error().data("error","新建失败");
        }
    }

    @Override
    public R updateBill(Bill bill) {

        Integer i= paymentTypeMapper.updateBill(bill);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","编辑失败");
        }

    }
}
